package com.viknix.threadpool.manager.cluster.listener;

import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Dongqi
 * @Date: 2021/11/11 13:40
 * @Version 1.0
 * @Description: SpringApplicationRunListener
 */
public class TpmSpringApplicationRunListener implements SpringApplicationRunListener, Ordered {

    private final SpringApplication application;

    private final String[] args;

    private List<TpmApplicationListener> tpmApplicationListeners = new ArrayList<>();

    public TpmSpringApplicationRunListener(SpringApplication application, String[] args) {
        this.application = application;
        this.args = args;
    }

    {
        tpmApplicationListeners.add(new StartingApplicationListener());
    }

    @Override
    public void starting(ConfigurableBootstrapContext bootstrapContext) {
        for (TpmApplicationListener tpmApplicationListener : tpmApplicationListeners) {
            tpmApplicationListener.starting();
        }
    }

    /**
     * 将 ConfigurableEnvironment 对象 注入 EnvUtil
     *
     * @param environment ConfigurableEnvironment 环境配置对象
     */
    @Override
    public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) {
        for (TpmApplicationListener tpmApplicationListener : tpmApplicationListeners) {
            tpmApplicationListener.environmentPrepared(environment);
        }
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        for (TpmApplicationListener tpmApplicationListener : tpmApplicationListeners) {
            tpmApplicationListener.contextPrepared(context);
        }
    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {
        for (TpmApplicationListener tpmApplicationListener : tpmApplicationListeners) {
            tpmApplicationListener.contextLoaded(context);
        }
    }

    @Override
    public void started(ConfigurableApplicationContext context) {
        for (TpmApplicationListener tpmApplicationListener : tpmApplicationListeners) {
            tpmApplicationListener.started(context);
        }
    }

    @Override
    public void running(ConfigurableApplicationContext context) {
        for (TpmApplicationListener tpmApplicationListener : tpmApplicationListeners) {
            tpmApplicationListener.running(context);
        }
    }

    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {
        for (TpmApplicationListener tpmApplicationListener : tpmApplicationListeners) {
            tpmApplicationListener.failed(context, exception);
        }
    }

    @Override
    public int getOrder() {
        return HIGHEST_PRECEDENCE;
    }
}
